package com.app.qbake.module.mine;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.nim.uikit.api.NimUIKit;
import com.wind.base.mvp.view.BaseLoadingFragment;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.ViewInflater;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.OrderDetailRequest;
import com.wind.data.mine.OrderDetailResponse;
import com.wind.data.mine.OrderListRequest;
import com.wind.data.mine.bean.Order;
import com.wind.data.mine.bean.OrderDetail;
import com.wind.data.msg.GoodsChatResponse;
import com.app.qbake.R;
import com.app.qbake.module.mine.activity.VipPkgActivity;
import com.app.qbake.module.mine.di.OrderDetailComponent;
import com.app.qbake.module.mine.mvp.OrderDetailView;
import com.app.qbake.module.mine.presenter.OrderDetailPresenter;
import com.app.qbake.module.msg.layout.GoodsChatLayout;
import com.app.qbake.util.AppDialogHelper;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.OrderDetailView",
                responseCanonicalName = "com.wind.data.mine.OrderDetailResponse",
                requestCanonicalName = "com.wind.data.mine.OrderDetailRequest"

        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "order/query")
)
public class OrderDetailFragment extends BaseLoadingFragment<OrderDetailView, OrderDetailPresenter, OrderDetailComponent, OrderDetailResponse>
        implements OrderDetailView, GoodsChatLayout.OnGoodsChatListener {

    public static final String ARGS_KEY_TYPE = "args_key_type";
    public static final String ARGS_KEY_ORDERID = "args_key_orderid";
    private String mType;
    private String mOrderId;
    @Inject
    OrderDetailPresenter presenter;

    @BindView(R.id.tv_order_status)
    TextView tv_order_status;
    @BindView(R.id.tv_order_price)
    TextView tv_order_price;
    @BindView(R.id.tv_name_phone)
    TextView tv_name_phone;
    @BindView(R.id.tv_addr)
    TextView tv_addr;

    @BindView(R.id.iv_goods)
    ImageView iv_goods;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_price)
    TextView tv_price;

    @BindView(R.id.tv_express_price)
    TextView tv_express_price;
    @BindView(R.id.tv_order_num)
    TextView tv_order_num;
    @BindView(R.id.tv_order_time)
    TextView tv_order_time;
    @BindView(R.id.tv_chat)
    TextView tv_chat;
    GoodsChatLayout mGoodsChatLayout;
    @Override
    public OrderDetailPresenter createPresenter() {
        return presenter;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mType = getArguments().getString(ARGS_KEY_TYPE);
        mOrderId = getArguments().getString(ARGS_KEY_ORDERID);
        super.onViewCreated(view, savedInstanceState);
        mGoodsChatLayout=new GoodsChatLayout(getActivity(),this);
    }

    @Override
    protected BaseRequest buildLoadingRequest() {
        OrderDetailRequest request = new OrderDetailRequest();
        request.setType(mType);
        request.setId(mOrderId);
        return request;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_order_detail;
    }
    private OrderDetail mOrderDetail;
    @Override
    public void showPage(OrderDetailResponse reponse) {
        mOrderDetail=reponse.getData();

        String status=getStatus(mOrderDetail);

        ViewInflater.inflateText(tv_order_status,status);
        ViewInflater.inflateText(tv_order_price,"￥"+mOrderDetail.getPrice());
        String name_phone=mOrderDetail.getUser_name()+" "+mOrderDetail.getUser_phone();
        ViewInflater.inflateText(tv_name_phone,name_phone);
        ViewInflater.inflateText(tv_addr,mOrderDetail.getUser_address());

        ViewInflater.inflateImage(iv_goods,mOrderDetail.getImg_url());
        ViewInflater.inflateText(tv_title,mOrderDetail.getTitle());
        ViewInflater.inflateText(tv_price,"￥"+mOrderDetail.getPrice());
        ViewInflater.inflateText(tv_express_price,"运费 ￥"+mOrderDetail.getPostage_price());
        ViewInflater.inflateText(tv_order_num,mOrderDetail.getOrder_no());
        ViewInflater.inflateText(tv_order_time,mOrderDetail.getCreated_at());


        mLayoutManager.showContent();
    }

    private String getStatus(OrderDetail orderDetail) {
        boolean ismail=orderDetail.getIs_mail()==1;
        String status="";
        if (mType.equals(OrderListRequest.TYPE_BUY)){
            tv_chat.setText("联系卖家");
            switch (orderDetail.getOrder_status()){
                case Order.STATUS_PAYFAILED:
                    status="支付失败";
                    break;
                case Order.STATUS_PENDINGPAY:
                    status="待支付";
                    break;
                case Order.STATUS_PAYED:
                    if (ismail){
                        status="待收货";
                    }else {
                        status="已支付";

                    }
                    break;
                case Order.STATUS_PENDINGCOMMENT:
                    status="待评论";
                    break;
                case Order.STATUS_REFUND:
                    status="退款";
                    break;
                case Order.STATUS_FINISH:
                    status="已结束";
                    break;
                case Order.STATUS_CLOSED:
                    status="已关闭";
                    break;
            }
        }else {
            tv_chat.setText("联系买家");
            switch (orderDetail.getOrder_status()){


                case Order.STATUS_PAYED://根据是否发货确定
                    if (!ismail){
                        status="已支付,待发货";

                    }else {
                        status="已发货";
                    }


                    break;
                case Order.STATUS_PENDINGCOMMENT:
                    status="待评论";

                    break;
                case Order.STATUS_REFUND:
                    status="退款";

                    break;
                case Order.STATUS_FINISH:
                    status="已结束";

                    break;
                case Order.STATUS_CLOSED:
                    status="已关闭";

                    break;
            }
        }
        return status;
    }


    public static Fragment getInstance(String type, String orderId) {
        Fragment f = new OrderDetailFragment();
        Bundle args = new Bundle();
        args.putString(ARGS_KEY_TYPE, type);
        args.putString(ARGS_KEY_ORDERID, orderId);
        f.setArguments(args);
        return f;
    }

    @OnClick(R.id.tv_chat)
    public void onViewClick(View v){
        mGoodsChatLayout.orderChat(mOrderId,mOrderDetail.getOrder_status()+"");
    }

    @Override
    public void onGoodsChatSuccess(GoodsChatResponse response) {
        if ("yes".equals(response.getData().getChat())){
            NimUIKit.startP2PSession(getActivity(), mOrderDetail.getShop_userId());
        }else {
            AppDialogHelper.showNormalDialog(getActivity(),
                    "只有VIP才能聊天", "取消", "开通", new AppDialogHelper.DialogOperCallback() {
                        @Override
                        public void onDialogConfirmClick() {

                            VipPkgActivity.start(getActivity());
                        }
                    });
        }
    }
}
